/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.plc;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageButton;

import com.android.plc.ui.ControlManager;
import com.android.plc.util.StringUtil;

public class Main extends Activity implements OnClickListener
{
	// adb shell monkey -v -p com.android.plc 500000
	protected static final String TAG = "Main";
	private ControlManager mControlManager;
	private Boolean mAdminMode = false;
	private View mSettingView;
	private static final String DEF_PASSWORD = "123456";
	private static final boolean DEBUG = false;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		Log.e(TAG, "start onCreate~~~");
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
		getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
		getWindow().setFormat(PixelFormat.RGBA_8888);
		
		setContentView(R.layout.main);

		StringUtil.initTypes(this);

		ImageButton camera = (ImageButton) findViewById(R.id.main_camera);
		camera.setOnClickListener(this);

		ViewGroup mainLayout = (ViewGroup) findViewById(R.id.main_layout);
		mControlManager = new ControlManager(this, mainLayout);

		// ProfileDetailView pdv = (ProfileDetailView)
		// findViewById(R.id.profile_detail);
		mSettingView = findViewById(R.id.main_admin);
		mSettingView.setOnClickListener(new OnClickListener()
		{
			public void onClick(View view)
			{
				if (mAdminMode)
				{
					setAdminMode(false);
				} else
				{
					LayoutInflater factory = LayoutInflater.from(Main.this);
					final View DialogView = factory.inflate(R.layout.admin_dialog, null);
					AlertDialog dlg = new AlertDialog.Builder(Main.this)
							.setTitle(getResources().getString(android.R.string.dialog_alert_title))
							.setView(DialogView)
							.setPositiveButton(getResources().getString(android.R.string.ok),
									new DialogInterface.OnClickListener()
									{
										public void onClick(DialogInterface dialog, int which)
										{
											EditText pwd = (EditText) DialogView.findViewById(R.id.password);
											if (DEF_PASSWORD.equals(pwd.getText().toString()))
											{
												setAdminMode(true);
											} else
											{
												AlertDialog.Builder builder = new AlertDialog.Builder(Main.this);
												builder.setTitle(
														getResources().getString(android.R.string.dialog_alert_title))
														.setPositiveButton(android.R.string.ok,
																new DialogInterface.OnClickListener()
																{
																	public void onClick(DialogInterface dialog,
																			int which)
																	{
																		dialog.dismiss();
																	}
																}).setMessage(R.string.password_not_correct).create();
												builder.show();
											}
										}
									})
							.setNegativeButton(getResources().getString(android.R.string.cancel),
									new DialogInterface.OnClickListener()
									{
										public void onClick(DialogInterface dialog, int which)
										{

										}
									}).create();
					if (DEBUG)
					{
						setAdminMode(true);
					} else
					{
						dlg.show();
					}
				}
			}
		});

		Object instance = getLastNonConfigurationInstance();
		boolean admin = false;
		if (instance != null && instance instanceof Boolean)
		{
			Log.d(TAG, "onResume getLastNonConfigurationInstance");
			admin = (Boolean) instance;
		}
		mAdminMode = admin;
	}

	@Override
	protected void onStart()
	{
		super.onStart();
		Log.e(TAG, "onStart~~~");
	}

	@Override
	protected void onPause()
	{
		super.onPause();
		Log.e(TAG, "onPause~~~");
	}

	@Override
	protected void onResume()
	{
		super.onResume();
		Log.e(TAG, "onResume~~~");
		setAdminMode(mAdminMode);
	}

	@Override
	protected void onStop()
	{
		super.onStop();
		Log.e(TAG, "onStop~~~");
	}

	private void setAdminMode(boolean admin)
	{
		mAdminMode = admin;
		mSettingView.setFocusable(admin);
		mSettingView.setPressed(admin);
		mControlManager.setAdminMode(admin);
	}

	@Override
	protected void onDestroy()
	{
		super.onDestroy();
		Log.e(TAG, "start onDestroy~~~");
		if (mControlManager != null)
		{
			mControlManager.release();
			mControlManager = null;
		}
	}

	@Override
	public Object onRetainNonConfigurationInstance()
	{
		return mAdminMode;
	}

	@Override
	public void onWindowFocusChanged(boolean hasFocus)
	{
		super.onWindowFocusChanged(hasFocus);
		Log.e(TAG, "onWindowFocusChanged~~~" + hasFocus);
		mSettingView.setPressed(mAdminMode);
	}

	public void onClick(View v)
	{
		switch (v.getId())
		{
		case R.id.main_camera:
			Intent intent = new Intent(Intent.ACTION_MAIN);
			intent.setClass(this, Camera.class);
			intent.addCategory(Intent.CATEGORY_DEFAULT);
			startActivity(intent);
			break;

		default:
			break;
		}
	}

	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
		{
			dialog();
		}
		return false;
	}

	protected void dialog()
	{
		AlertDialog.Builder builder = new Builder(Main.this);
		builder.setMessage(R.string.tips_quit);
		builder.setTitle(R.string.tips);
		builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener()
		{
			public void onClick(DialogInterface dialog, int which)
			{
				dialog.dismiss();
				Main.this.finish();
			}
		});
		builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener()
		{
			public void onClick(DialogInterface dialog, int which)
			{
				dialog.dismiss();
			}
		});
		builder.create().show();
	}
}
